'use strict';
/**
 * Created by QianQi on 2017/1/17.
 */
module.exports = function(gulp,common){
    var plugin = common.plugin,
        config = common.config,
        wiPlugin = common.wiPlugin;
    var mainSrc=config['src']+'wiFrame/',// 源目录
        mainDest=config['dest']+'wiFrame/',// 目标目录
        mainTemp=config['temp']+'wiFrame/',// 临时目录
        wiFrameComp=config['wiFrameComp'];// 组件名称数组
    /** ============ **/
    gulp.task('wiFrame',plugin.sequence(
        'wiFrame_jsComponents','wiFrame_jsReplace',
        'wiFrame_css','wiFrame_pic',
        'wiFrame_demo',
        'wiFrame_docCopy','wiFrame_doc',
        'wiFrame_clean'
    ));
    /* js */
    gulp.task('wiFrame_jsComponents',function(){
        var aPath = wiFrameComp.map(function(item,i){
            return mainSrc+item+'/com.js';
        });
        return gulp
            .src(aPath)
            .pipe(plugin.concat('components.js'))//合并后所有组件代码
            .pipe(gulp.dest(mainTemp));//临时文件
    });
    gulp.task('wiFrame_jsReplace',function(){
        //process.argv
        //console.log(progress.argv);
        return gulp
            .src(mainSrc+'_com/common.js')// 包含替换字符串的源文件
            .pipe(wiPlugin.replace({
                original:{
                    pro: /\/\*@{3}PRO@{3}\*\//g,// 原型扩展
                    components: /\/\*@{3}COMPONENTS@{3}\*\//g,// 源文件中将被替换的字符串
                    compatible:/\/\*@{3}COMPATIBLE@{3}\*\//g// 兼容代码
                },
                target:{
                    pro: mainSrc+'_com/pro.js',// 原型扩展
                    components: mainTemp+'components.js',// 包含替换内容的文件
                    compatible: mainSrc+'_com/compatible.js'// 兼容旧版本的代码
                }
            }))
            .pipe(plugin.rename({basename:'common-all'}))
            .pipe(gulp.dest(mainDest))// 开发版本
            .pipe(plugin.uglify())
            .pipe(plugin.rename({basename:'common'}))
            .pipe(gulp.dest(mainDest));// 压缩版本
    });
    /* themes */
    gulp.task('wiFrame_css',function(){
        var aPath = wiFrameComp.map(function(item,i){
            return mainSrc+'_themes/css/'+item+'.css';
        });
        aPath.unshift(mainSrc+'_themes/css/_com.css');//基础样式
        return gulp
            .src(aPath)
            //.pipe(plugin.less())
            .pipe(plugin.concat('common-all.css'))
            .pipe(gulp.dest(mainDest+'css'))// 开发版本
            .pipe(plugin.minifyCss())
            .pipe(plugin.rename({basename:'common'}))
            .pipe(gulp.dest(mainDest+'css'));// 压缩版本
    });
    gulp.task('wiFrame_pic',function(){
        return gulp
            .src([mainSrc+'_themes/{icons,images}/**/*'],{
                base:mainSrc+'_themes'
            })
            .pipe(plugin.imagemin())// 图片压缩
            .pipe(gulp.dest(mainDest));
    });
    /* doc */
    gulp.task('wiFrame_docCopy',function(){
        var sDocSrc=config.src+'doc/';
        return gulp
            .src([sDocSrc+'**/*','!'+sDocSrc+'api','!'+sDocSrc+'js/indexData.js'],{
                base:config.src
            })
            .pipe(gulp.dest(config.dest));
    });
    gulp.task('wiFrame_doc',function(){
        return gulp
            .src(mainDest+'common-all.js')// 组件完整代码路径
            // 生成各功能 api 页面及菜单
            .pipe(wiPlugin.wiFrameDoc({
                dempPrev:'../../demo/',// demo 文件路径访问前缀
                apiTemp:config.src+'doc/api/template.html',// api 模板文件
                apiMenu:config.dest+'doc/js/indexData.js',
                apiDest:config.dest+'doc/api/'// 生成的组件 api 的 html 文件目录
            }))
            .pipe(plugin.rename('indexData.js'))
            .pipe(gulp.dest(config.dest+'doc/js'));
    });
    /* demo */
    gulp.task('wiFrame_demo',function(){
        var aPath = wiFrameComp.map(function(item,i){
            return config.src+'demo/**/*';
        });
        return gulp
            .src(aPath)
            .pipe(gulp.dest(config.dest+'demo'));
    });
    /* 清理临时目录 */
    gulp.task('wiFrame_clean',function(){
        return gulp
            .src(mainTemp,{read: false})
            .pipe(plugin.clean());// 通过返回 stream 保证依赖
    });
};